package com.hluck.goodweatherapp.data.db;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.hluck.goodweatherapp.data.bean.MyCity;
import com.hluck.goodweatherapp.data.bean.Province;
import com.hluck.goodweatherapp.data.db.dao.MyCityDao;
import com.hluck.goodweatherapp.data.db.dao.ProvinceDao;

import org.jetbrains.annotations.NotNull;

/**
 * @Author： LJH
 * @Time： 2024/1/4
 * @description：
 */
@Database(entities = {Province.class, MyCity.class},version = 2,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "Goodweather";
    private static volatile AppDatabase mInstance;

    public abstract ProvinceDao provinceDao();

    public abstract MyCityDao myCityDao();

    static final Migration MIGRATION_1_2 = new Migration(1,2) {
        @Override
        public void migrate(@NonNull @NotNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE 'myCity' (cityName TEXT NOT NULL, PRIMARY KEY('cityName'))");
        }
    };

    public static AppDatabase getInstance(Context context) {
        if (mInstance == null){
            synchronized (AppDatabase.class){
                if (mInstance == null){
                    mInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,DATABASE_NAME)
                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return mInstance;
    }
}
